﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Hxsoft.AppV1;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.Admin.RoleManage
{
	public partial class Accredit : BasePage
	{
		private LoadedValue<Guid> loadedIdentifier;
		public Guid Identifier
		{
			get
			{
				if (loadedIdentifier.Loader == null)
					loadedIdentifier.Loader = () => new Guid(Request.QueryString["RoleId"]);
				return loadedIdentifier.Value;
			}
		}

		private Guid _categoryId;
		private bool _isSetCategoryId = false;
		/// <summary>
		/// 当前资源类别代码。
		/// </summary>
		protected Guid CategoryId
		{
			get
			{
				if (!_isSetCategoryId)
				{
					string strCategoryId = Request.QueryString["CategoryId"];
					if (!string.IsNullOrEmpty(strCategoryId))
						_categoryId = new Guid(strCategoryId);
					else
					{
						List<ResourceCategory> resourceCategorys = ResourceCategories.GetItems();
						if (resourceCategorys.Count > 0) _categoryId = resourceCategorys[0].CategoryId;
					}
					_isSetCategoryId = true;
				}

				return _categoryId;
			}
		}

		private LoadedValue<Role> loadedRoleInfo;
		/// <summary>
		/// 专题信息。
		/// </summary>
		public virtual Role RoleInfo
		{
			get
			{
				if (loadedRoleInfo.Loader == null)
					loadedRoleInfo.Loader = () => Roles.GetItem(Identifier);
				return loadedRoleInfo.Value;
			}
		}

		/// <summary>
		/// 当前用户标识。
		/// </summary>
		protected Guid UserId
		{
			get
			{
				return Users.CurrentUserId.Value;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				Page.DataBind();
				if (RightValidate())
					InitData();
			}
		}

		protected void BtSubmit_Click(object sender, EventArgs e)
		{
			if (RightValidate() && IsValid)
			{
				List<PermissionState> permissionsStatus = UCAccredit.PermissionsStatus;

				Roles.SetRolePermissions(Identifier, permissionsStatus);
				OperateComplete();
			}
		}

		/// <summary>
		/// 初始化数据。
		/// </summary>
		public void InitData()
		{
			UCAccredit.PermissionsStatus = Roles.GetRolePermissions(Identifier);
		}

		/// <summary>
		/// 返回权限验证结果，如果验证失败则显示消息提示。
		/// </summary>
		/// <returns>布尔值代表权限验证是否通过。</returns>
		public bool RightValidate()
		{
			bool right = Roles.RoleHasProperty(Identifier, Roles.RolePropertyCode.DisallowRoleAccredit);
			if (right)
			{
				Messages.Add(new PageMessage(false, null, PageMessageType.Error, Res.PermissionExceptionMessageTitle, Res.PermissionExceptionMessage));
				return !right;
			}

			right = Roles.GetAccreditRight(Roles.GetItem(Identifier));
			if (!right)
			{
				Messages.Add(new PageMessage(false, null, PageMessageType.Error, Res.PermissionExceptionMessageTitle, Res.PermissionExceptionMessage));
			}
			return right;
		}

		/// <summary>
		/// 授权操作完成，显示消息提示。
		/// </summary>
		public void OperateComplete()
		{
			Messages.Add(new PageMessage(true, null, PageMessageType.Prompt, Res.OperationCompleteTitle, Res.OperationComplete_RoleAccredit));
		}
	}
}
